<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input v-model="formData.name" placeholder="请输入数据标题，不超过50个字符" />
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        @blur="specialFormValidate('desc')"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="大小">
      <el-input
        min="0"
        v-model.number="formData.attr.size"
        placeholder=""
        @blur="specialFormValidate('size')"
        @keyup.enter.native="specialFormValidate('size')"
      />
    </el-form-item>
    <el-form-item label="填充">
      <el-color-picker v-model="formData.attr.color" @change="specialFormValidate('color')" />
      <div class="row-item-small">
        <el-input
          v-model="formData.attr.color"
          placeholder=""
          @blur="specialFormValidate('color')"
          @keyup.enter.native="specialFormValidate('color')"
        />
      </div>
    </el-form-item>
    <el-form-item label="线段">
      <el-color-picker
        v-model="formData.attr.outlineColor"
        @change="specialFormValidate('outlineColor')"
      />
      <div class="row-item-small">
        <el-input
          v-model="formData.attr.outlineColor"
          placeholder=""
          @blur="specialFormValidate('outlineColor')"
          @keyup.enter.native="specialFormValidate('outlineColor')"
        />
      </div>
      <div class="form-item-label-right">线宽</div>
      <div class="outline">
        <el-input-number
          v-model="formData.attr.outlineWidth"
          :min="0"
          controls-position="right"
          @blur="specialFormValidate('outlineWidth')"
          @keyup.enter.native="specialFormValidate('outlineWidth')"
        />
      </div>
    </el-form-item>
    <el-form-item label="高度">
      <div class="row-item">
        <el-input
          v-model="convertObj.height"
          @blur="specialFormValidate('height')"
          @keyup.enter.native="specialFormValidate('height')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item>
    <el-form-item label="可视距离">
      <div class="row-item">
        <el-input
          v-model="convertObj.minDisplayDistance"
          :min="0"
          @blur="specialFormValidate('minDisplayDistance')"
          @keyup.enter.native="specialFormValidate('minDisplayDistance')"
        />
        <div class="unit">米</div>
        <div class="unit">—</div>
        <el-input
          v-model="convertObj.maxDisplayDistance"
          @blur="specialFormValidate('maxDisplayDistance')"
          @keyup.enter.native="specialFormValidate('maxDisplayDistance')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item>
    <el-form-item label="跟随缩放" class="border-top">
      <div class="shrink" @click="shrink('scale')">
        <SvgIcon v-if="shrinkFlag['scale']" icon-class="ic_向上" />
        <SvgIcon v-else icon-class="ic_向下" />
      </div>
      <div class="line"></div>
      <div v-if="shrinkFlag['scale']">
        <div class="row">
          <div class="row-col">显示距离</div>
          <div class="row-col">缩放范围(m)</div>
          <div class="row-col">缩放比例(%)</div>
        </div>
        <div class="row">
          <div class="row-col">最小距离</div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.scaleByDistanceArray[0]"
              :min="0"
              @blur="specialFormValidate('scaleByDistanceArray')"
              @keyup.enter.native="specialFormValidate('scaleByDistanceArray')"
            />
          </div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.scaleByDistanceArray[1]"
              :min="0"
              @blur="specialFormValidate('scaleByDistanceArray')"
              @keyup.enter.native="specialFormValidate('scaleByDistanceArray')"
            />
          </div>
        </div>
        <div class="row">
          <div class="row-col">最大距离</div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.scaleByDistanceArray[2]"
              :min="0"
              @blur="specialFormValidate('scaleByDistanceArray')"
              @keyup.enter.native="specialFormValidate('scaleByDistanceArray')"
            />
          </div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.scaleByDistanceArray[3]"
              :min="0"
              @blur="specialFormValidate('scaleByDistanceArray')"
              @keyup.enter.native="specialFormValidate('scaleByDistanceArray')"
            />
          </div>
        </div>
      </div>
    </el-form-item>
    <el-form-item label="跟随透明" class="border-top">
      <div class="shrink" @click="shrink('translucency')">
        <SvgIcon v-if="shrinkFlag['translucency']" icon-class="ic_向上" />
        <SvgIcon v-else icon-class="ic_向下" />
      </div>
      <div class="line"></div>
      <div v-if="shrinkFlag['translucency']">
        <div class="row">
          <div class="row-col">显示距离</div>
          <div class="row-col">缩放范围(m)</div>
          <div class="row-col">透明度(%)</div>
        </div>
        <div class="row">
          <div class="row-col">最小距离</div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.translucencyByDistanceArray[0]"
              :min="0"
              @blur="specialFormValidate('translucencyByDistanceArray')"
              @keyup.enter.native="specialFormValidate('translucencyByDistanceArray')"
            />
          </div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.translucencyByDistanceArray[1]"
              :min="0"
              @blur="specialFormValidate('translucencyByDistanceArray')"
              @keyup.enter.native="specialFormValidate('translucencyByDistanceArray')"
            />
          </div>
        </div>
        <div class="row">
          <div class="row-col">最大距离</div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.translucencyByDistanceArray[2]"
              :min="0"
              @blur="specialFormValidate('translucencyByDistanceArray')"
              @keyup.enter.native="specialFormValidate('translucencyByDistanceArray')"
            />
          </div>
          <div class="row-col">
            <el-input
              v-model.number="convertObj.translucencyByDistanceArray[3]"
              :min="0"
              @blur="specialFormValidate('translucencyByDistanceArray')"
              @keyup.enter.native="specialFormValidate('translucencyByDistanceArray')"
            />
          </div>
        </div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const emits = defineEmits(['formValidate']);
const shrinkFlag = reactive<any>({
  scale: false,
  translucency: false,
});
const convertObj = ref<any>({
  scaleByDistanceArray: [0, 0, 0, 0],
  translucencyByDistanceArray: [0, 0, 0, 0],
  height: '0.00',
  minDisplayDistance: '0.00',
  maxDisplayDistance: '0.00',
});

function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['size', 'outlineWidth'].includes(fieldName)) {
    if (!(formData.value.attr[fieldName] > 0)) valid = false;
  }
  if ('color' === fieldName) {
    if (!formData.value.attr[fieldName]) formData.value.attr[fieldName] = '#FFFFFF'; //默认白色
  }
  if ('outlineColor' === fieldName) {
    if (!formData.value.attr[fieldName]) formData.value.attr[fieldName] = '#000000'; // 默认黑色
  }
  if (['height', 'minDisplayDistance', 'maxDisplayDistance'].includes(fieldName)) {
    if ('' === formData.value.attr[fieldName] || isNaN(formData.value.attr[fieldName])) {
      valid = false;
    } else {
      formData.value.attr[fieldName] = +convertObj.value[fieldName];
    }
  }
  if (['minDisplayDistance', 'maxDisplayDistance'].includes(fieldName)) {
    if (
      !(
        formData.value.attr['minDisplayDistance'] >= 0 &&
        formData.value.attr['maxDisplayDistance'] >= 0
      )
    ) {
      valid = false;
    }
  }
  if (['scaleByDistanceArray', 'translucencyByDistanceArray'].includes(fieldName)) {
    if (!formData.value.attr[fieldName].every((ele: number) => ele >= 0)) {
      valid = false;
    } else {
      formData.value.attr[fieldName][1] = convertObj.value[fieldName][1] / 100;
      formData.value.attr[fieldName][3] = convertObj.value[fieldName][3] / 100;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  emits('formValidate', fieldName, valid);
}

watchEffect(() => {
  const { cimInstance } = window;
  const layer = props.form;
  const config = cimInstance.api.getCurrentLayerConfig(layer.layerName);
  console.log('-----config-----', config);
  layer.attr = {
    size: config.size,
    color: config.color,
    outlineColor: config.outlineColor,
    outlineWidth: config.outlineWidth,
    scaleByDistanceArray: config.scaleByDistanceArray,
    translucencyByDistanceArray: config.translucencyByDistanceArray,
    minDisplayDistance: config.minDisplayDistance,
    maxDisplayDistance: config.maxDisplayDistance,
    height: config.height,
  };
  updateObj(config.scaleByDistanceArray, 'scaleByDistanceArray');
  updateObj(config.translucencyByDistanceArray, 'translucencyByDistanceArray');
  convertObj.value.height = config.height && config.height.toFixed(2);
  convertObj.value.minDisplayDistance =
    config.minDisplayDistance && config.minDisplayDistance.toFixed(2);
  convertObj.value.maxDisplayDistance =
    config.maxDisplayDistance && config.maxDisplayDistance.toFixed(2);
  formData.value = layer;
  console.log('---convertObj---', convertObj, layer);
});

function updateObj(arr: any, str: string) {
  if (arr?.length > 0) {
    convertObj.value[str] = [arr[0], 100 * arr[1], arr[2], 100 * arr[3]];
  }
}

function shrink(str: string) {
  shrinkFlag[str] = !shrinkFlag[str];
}
</script>

<style lang="less" scoped>
.el-form {
  .row-item-small {
    width: 100px;
    margin-left: 10px;
    line-height: normal;
  }
  .form-item-label-right {
    position: absolute;
    top: -35px;
    left: 170px;
  }
  .shrink {
    position: absolute;
    font-size: 15px;
    top: -34px;
    right: 0;
    width: 30px;
    display: flex;
    justify-content: center;
    height: 30px;
    align-items: center;
    cursor: pointer;
  }

  .outline {
    width: 120px;
    margin-left: 28px;
    line-height: normal;

    .el-input__inner {
      text-align: left;
    }
  }

  .line {
    height: 1px;
    background: #1f2329;
    width: calc(100% + 40px);
    margin-left: -20px;
    margin-right: -20px;
  }

  .line:not(div:last-of-type) {
    margin-bottom: 10px;
  }

  .row {
    width: 100%;
    display: flex;
    margin-bottom: 10px;

    &:last-of-type {
      margin-bottom: 0;
    }

    .row-col {
      width: 34%;
    }

    .row-col:last-of-type {
      margin-left: 20px;
    }
  }

  .row-item {
    width: 100%;
    display: flex;

    .el-input {
      flex: 1;
    }

    .unit {
      flex: 0 0 24px;
      text-align: center;
    }
  }
}
</style>
